// @ts-ignore
import appsettings from '@/appsettings';
import { createRouter, createWebHistory, RouteRecordRaw,createWebHashHistory } from 'vue-router';


let childrenPageRoutes: RouteRecordRaw[] = [
    {
        path: '/Dashboard/Analysis',
        name: 'Analysis',
        meta: { title: "分析页面" },
        props: true,
        component: () => import('@/pages/Dashboard/Analysis.vue')
    },
    {
        path: '/demo/list',
        name: 'List',
        meta: { title: "列表页面示例" },
        props: true,
        component: () => import('@/pages/Demo/List.vue')
    },
    {
        path: '/demo/form',
        name: 'Form',
        meta: { title: "表单列表示例" },
        props: true,
        component: () => import('@/pages/Demo/Form.vue')
    },

];

/**
 * 自动注册页面路由
 */
// const registerPageRouters = function () {
//     const modules = import.meta.glob('../pages/**/*.vue');
//     const components = import.meta.globEager('../pages/**/*.vue');

//     Object.keys(modules).forEach(key => {
//         const viewSrc = components[key];
//         const file = viewSrc.default;
//         console.log(modules);
//         // /(?<=pages).*?(?=.vue)/
//         let pathArray = String(file.__file).match(/(?<=pages\/).*?(?=.vue)/img) ?? [""];
//         let path = pathArray[0];
//         if (path != "") {
//             childrenPageRoutes.push({
//                 path: path,
//                 name: path,
//                 props: true,
//                 component: modules[key]
//             });
//         }
//     })
// }

//@@/page/sss/{}
//registerPageRouters();

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        name: 'Layout',
        meta: { title: appsettings.ProjectName },
        component: () => import('@/pages/Share/Layout.vue'),
        children: childrenPageRoutes,
        redirect: appsettings.HomePagePath,
        props: true
    },
    {
        path: '/user/login',
        name: 'UserLogin',
        meta: { title: "用户登录" },
        props: true,
        component: () => import('@/pages/User/login.vue')
    },
    {
        path: '/user/register',
        name: 'UserRegister',
        props: true,
        meta: { title: "用户注册" },
        component: () => import('@/pages/User/Register.vue')
    },
    {
        path: '/exception/401',
        name: '401',
        meta: { title: "无效授权方式" },
        component: () => import('@/pages/Share/Exception/401.vue')
    },
    {
        path: '/exception/404',
        name: '404',
        meta: { title: "找不到页面 -_-||" },
        component: () => import('@/pages/Share/Exception/404.vue')
    },
    {
        path: "/:pathMatch(.*)*",
        name: "NotFound",
        redirect: "/exception/404"
    }
];


const router = createRouter({
    history: createWebHashHistory(),
    routes: routes
});

router.afterEach((to: any, _) => {
    window.document.title = to.meta.title;
})

export default router;
